* This do-file creates masterfiles of baseline characteristics and attrition for survey HHs


tempfile roster


*** Household controls and interactions for heterogeneity analysis

* HH head characteristics, wealth index (winsorized), and previous pulse experience
use district block village hhid pulses_previous wealthindex age gender caste using "$k1/Farm/main.dta", clear

rename age m_age
rename gender m_gender

rename pulses_previous bl_pulse_prev
winsor wealthindex, gen(bl_wealth_win5) highonly p(.05)
drop wealthindex
save `roster', replace

* Land ownership
use hhid plotno status_kharif plot_area_acre using "$k1/Farm/plots.dta", clear

* Restrict to farmed plots (owned, rented, or sharecropped)
keep if inlist(status_kharif,1,2,3,7)
collapse (sum) land=plot_area_acre, by(hhid) fast
// Winsorize land area
winsor land, gen(bl_land_win5) highonly p(.05)
drop land

merge 1:1 hhid using `roster', assert(match using)
recode bl_land (.=0)
drop _m

* Prior farmer group membership
merge 1:1 hhid using "$admin_data/mis_membership.dta", assert(match master) keepusing(Farmergroupmember_ever)
drop _m			// Unmerged from control only
rename Farmergroupmember_ever bl_group
recode bl_group .=0
 
 * Merge in HH head education from year 2 (not asked in year 1)
merge 1:1 hhid using "$k2/Farm/main.dta", assert(match master) keepusing(education) nogen
rename education m_education
// 52 households attrited between year 1 and year 2, master only

 * Merge in responses from Food survey
merge 1:1 hhid using "$k1/Food/consumption.dta", keepusing(age education gender hh_size) nogen
	// 4 from master (K17 Farm), not in using (K17 food)?
rename (age gender education) (f_age f_gender f_education)

order district block village hhid caste hh_size m_age m_education m_gender f_age f_education f_gender ///
	bl_pulse bl_wealth bl_land bl_group 

* Save baseline characteristics
save "$out_data/survey_roster.dta", replace
	
*** Flag attrition by round
use "$admin_data/survey_list.dta", clear
forvalues y = 1/3 {
	foreach season in kharif rabi {
		if (`y'==1 & "`season'"=="kharif") continue
		
		local s = substr("`season'",1,1)
		
		
		merge 1:1 hhid using "${`s'`y'}/Farm/main.dta", ///
			keepusing(survey_date) gen(drop_`s'`y's) assert(match master)
		recode drop_`s'`y's (3=0)
		drop survey_date
	}
}

rename drop_r3s drop_r3p
egen drop_srvy = rowmax(drop_*s)
egen drop_ever = rowmax(drop_*)
save "$out_data/survey_drops.dta", replace

